package org.iabako.server.controller.timer.demo;

import org.apache.log4j.Logger;
import org.iabako.server.dao.user.UserDAO;
import org.iabako.shared.entity.enumeration.Language;
import org.iabako.shared.entity.user.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by jose on 06/01/15.
 */

@Controller
public class InstallDemoDBControllerTimer {

    static Logger log = Logger.getLogger(InstallDemoDBControllerTimer.class.getName());

    private final String ENTERPRISE_CSV_EN = "/org/iabako/demo/ENTERPRISE_EN.csv";
    private final String ENTERPRISE_CSV_ES = "/org/iabako/demo/ENTERPRISE_ES.csv";
    private final String ENTERPRISE_CSV_FR = "/org/iabako/demo/ENTERPRISE_FR.csv";
    private final String CLIENT_CSV = "/org/iabako/demo/CLIENT.csv";
    private final String PRODUCT_CSV_EN = "/org/iabako/demo/PRODUCT_EN.csv";
    private final String PRODUCT_CSV_ES = "/org/iabako/demo/PRODUCT_ES.csv";
    private final String PRODUCT_CSV_FR = "/org/iabako/demo/PRODUCT_FR.csv";
    private final String SERVICE_CSV_EN = "/org/iabako/demo/SERVICE_EN.csv";
    private final String SERVICE_CSV_ES = "/org/iabako/demo/SERVICE_ES.csv";
    private final String SERVICE_CSV_FR = "/org/iabako/demo/SERVICE_FR.csv";

    @Autowired
    CreateUserController createUserController;
    @Autowired
    CreateClientController createClientController;
    @Autowired
    CreateSaleController createSaleController;
    @Autowired
    CreateExpenseController createExpenseController;
    @Autowired
    UserDAO userDAO;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    // every 21th of every month at 7AM
    @Scheduled(cron = "0 0 7 21 * ?")
    //@Scheduled(initialDelay = 10000, fixedRate = 3600000)
    public void actionMonthly() throws Exception {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (calendar.get(Calendar.MONTH) % 2 != 0) {
            //Only pair months (january, mars...)
            return;
        }

        log.info("Executing InstallDemoDBControllerTimer.create()...");

        log.info("Delete of DEMO data starts...");

        userDAO.deleteDemoData();

        log.info("Delete of DEMO data ends successfully");

        log.info("Insert of DEMO data starts...");

        Enterprise enterpriseEN = createUserController.create(ENTERPRISE_CSV_EN, Language.EN);
        Enterprise enterpriseES = createUserController.create(ENTERPRISE_CSV_ES, Language.ES);
        Enterprise enterpriseFR = createUserController.create(ENTERPRISE_CSV_FR, Language.FR);

        createClientController.create(enterpriseEN, CLIENT_CSV);
        createClientController.create(enterpriseES, CLIENT_CSV);
        createClientController.create(enterpriseFR, CLIENT_CSV);

        createSaleController.create(enterpriseEN, PRODUCT_CSV_EN, SERVICE_CSV_EN, CLIENT_CSV);
        createSaleController.create(enterpriseES, PRODUCT_CSV_ES, SERVICE_CSV_ES, CLIENT_CSV);
        createSaleController.create(enterpriseFR, PRODUCT_CSV_FR, SERVICE_CSV_FR, CLIENT_CSV);

        createExpenseController.create(enterpriseEN, Language.EN);
        createExpenseController.create(enterpriseES, Language.ES);
        createExpenseController.create(enterpriseFR, Language.FR);

        log.info("Insert of DEMO data ends successfully");
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    // every day at 8AM
    @Scheduled(cron = "0 0 8 * * ?")
    //@Scheduled(initialDelay = 60000, fixedRate = 60000)
    public void actionDaily() throws Exception {

        log.info("Executing InstallDemoDBControllerTimer.actionDaily()...");

        Enterprise enterpriseEN = createUserController.getEnterpriseFromCSV(ENTERPRISE_CSV_EN);
        Enterprise enterpriseES = createUserController.getEnterpriseFromCSV(ENTERPRISE_CSV_ES);
        Enterprise enterpriseFR = createUserController.getEnterpriseFromCSV(ENTERPRISE_CSV_FR);

        enterpriseEN = userDAO.getEnterpriseByUserEmail(enterpriseEN.getUsers().get(0).getEmail());
        enterpriseES = userDAO.getEnterpriseByUserEmail(enterpriseES.getUsers().get(0).getEmail());
        enterpriseFR = userDAO.getEnterpriseByUserEmail(enterpriseFR.getUsers().get(0).getEmail());

        createSaleController.initialize();
        createSaleController.createDailySales(enterpriseEN, PRODUCT_CSV_EN, SERVICE_CSV_EN, CLIENT_CSV);
        createSaleController.initialize();
        createSaleController.createDailySales(enterpriseES, PRODUCT_CSV_ES, SERVICE_CSV_ES, CLIENT_CSV);
        createSaleController.initialize();
        createSaleController.createDailySales(enterpriseFR, PRODUCT_CSV_FR, SERVICE_CSV_FR, CLIENT_CSV);

        createExpenseController.createDailyExpenses(enterpriseEN, Language.EN);
        createExpenseController.createDailyExpenses(enterpriseES, Language.ES);
        createExpenseController.createDailyExpenses(enterpriseFR, Language.FR);
    }
}

